package org.example.gui.panel;

import org.example.gui.listener.ToolBarListener;
import org.example.util.CenterPanel;
import org.example.util.GUIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * 界面类 MainPanel 主界面
 * 主要有一个 Toolbar 的工具栏 和 一个  workingPanel 的 CenterPanel 的 工作区
 * workingPanel工作区内为本包中的各个页面，它们都是抽象类 WorkingPanel 的 子类
 * 各个页面有它们独自的监听器负责用户操作的监听
 */

public class MainPanel extends JPanel {
    public static MainPanel instance = new MainPanel();
    public JButton bSpend = new JButton();
    public JButton bRecord = new JButton();
    public JButton bHistory = new JButton();
    public JButton bCategory = new JButton();
    public JButton bReport = new JButton();
    public JButton bConfig = new JButton();
    public JButton bBackup = new JButton();


    public CenterPanel workingPanel;

    private MainPanel() {

        GUIUtil.setImageIcon(bSpend, "home.png", "本月消费");
        GUIUtil.setImageIcon(bRecord, "record.png", "记账");
        GUIUtil.setImageIcon(bHistory, "history.png", "历史消费");
        GUIUtil.setImageIcon(bReport, "report.png", "统计");
        GUIUtil.setImageIcon(bCategory, "category.png", "分类");
        GUIUtil.setImageIcon(bConfig, "config.png", "设置");
        GUIUtil.setImageIcon(bBackup, "backup.png", "备份/恢复");


        JToolBar toolBar = new JToolBar();
        toolBar.setLayout(new GridLayout(1,7));

        toolBar.add(bSpend);
        toolBar.add(bRecord);
        toolBar.add(bHistory);
        toolBar.add(bReport);
        toolBar.add(bCategory);
        toolBar.add(bConfig);
        toolBar.add(bBackup);
        toolBar.setFloatable(false);

        workingPanel = new CenterPanel(0.95);

        this.setLayout(new BorderLayout());
        this.add(toolBar, BorderLayout.NORTH);
        this.add(workingPanel, BorderLayout.CENTER);

        addListeners();
    }


    private void addListeners() {
        ToolBarListener listener = new ToolBarListener();
        bSpend.addActionListener(listener);
        bHistory.addActionListener(listener);
        bBackup.addActionListener(listener);
        bCategory.addActionListener(listener);
        bConfig.addActionListener(listener);
        bRecord.addActionListener(listener);

        bReport.addActionListener(listener);
    }
}
